package com.fuyd.pms.background.web.account.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
 * 角色过滤器
 * Created by fuyongde on 2016/1/3.
 */
public class AnyOfRolesAuthorizationFilter extends RolesAuthorizationFilter {

    /**
     * 包含其中任意角色，即认证通过
     *
     * @param request     请求
     * @param response    响应
     * @param mappedValue 角色的列表
     * @return
     * @throws IOException
     */
    @Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {

        final Subject subject = getSubject(request, response);
        final String[] rolesArray = (String[]) mappedValue;

        if (rolesArray == null || rolesArray.length == 0) {
            //no roles specified, so nothing to check - allow access.
            return true;
        }

        for (String roleName : rolesArray) {
            if (subject.hasRole(roleName)) {
                return true;
            }
        }

        return false;
    }
}
